/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.intersoft.sandbox.ejb.logic;

import hu.intersoft.sandbox.ejb.entity.News;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * For Managing News Entity.
 * @author ntb
 */
@Stateless(mappedName = "SandboxManager")
public class SandboxManager implements SandboxManagerRemote {

    @PersistenceContext(unitName = "Sandbox-EjbPU")
    private EntityManager em;

    public List<News> getNewsOrderByDate(){
        return (List<News>) em.createNamedQuery("News.findAllOrderByDate").setMaxResults(3).getResultList();
    }

    public List<News> getNewsEntities(){
        return (List<News>) em.createNamedQuery("News.findAll").getResultList();
    }

    public List<News> getNewsEntitiesByTag(String tag){
        return (List<News>) em.createNamedQuery("News.findByTagLike").setParameter("tag", tag).getResultList();
    }

    public List<News> getNewsEntitiesByHeader(String header){
        return (List<News>) em.createNamedQuery("findByHeaderLike").setParameter("header", header).getResultList();
    }

    public int addNews(News news){
        em.persist(news);
        return news.getId();
    }

    public void removeNews(int newsId){
        News tmp = (News) em.createNamedQuery("News.findById").setParameter("id", newsId).getSingleResult();
        em.remove(tmp);
    }
}
